const { Model, DataTypes } = require("sequelize");
const sequelize = require("../app/database");
const Question = require("./question.model");
const User = require("./user.model");

class QuestionUser extends Model {}

QuestionUser.init(
  {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true,
    },
    question_id: {
      type: DataTypes.INTEGER,
      references: {
        model: Question,
        key: "id",
      },
    },
    user_id: {
      type: DataTypes.INTEGER,
      references: {
        model: User,
        key: "id",
      },
    },
    familiar: {
      type: DataTypes.INTEGER,
    },
    analysis: {
      type: DataTypes.INTEGER,
    },
    experience: {
      type: DataTypes.INTEGER,
    },
    reference_data: {
      type: DataTypes.INTEGER,
    },
    intuition: {
      type: DataTypes.INTEGER,
    },
    max_value: {
      type: DataTypes.INTEGER,
    },
    possible_value: {
      type: DataTypes.INTEGER,
    },
    min_value: {
      type: DataTypes.INTEGER,
    },
    max_reason: {
      type: DataTypes.STRING,
    },
    possible_reason: {
      type: DataTypes.STRING,
    },
    min_reason: {
      type: DataTypes.STRING,
    },
    answer_round: {
      type: DataTypes.INTEGER,
    },
    config_result: {
      type: DataTypes.STRING,
    },
    createAt: {
      type: DataTypes.DATE,
    },
    updateAt: {
      type: DataTypes.DATE,
    },
  },
  {
    tableName: "question_user",
    createdAt: "createAt",
    updatedAt: "updateAt",
    timestamps: true,
    sequelize,
  }
);

Question.belongsToMany(User, {
  through: QuestionUser,
  foreignKey: "question_id",
  otherKey: "user_id",
});

User.belongsToMany(Question, {
  through: QuestionUser,
  foreignKey: "user_id",
  otherKey: "question_id",
});

module.exports = QuestionUser;
